package com.mezz.bones.framework.payment.wxpay;

import cn.hutool.core.io.resource.ClassPathResource;
import com.mezz.bones.framework.payment.wxpay.WxpayProperties;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;


@ConditionalOnProperty(prefix = "pay",name = "wxpay.app-id")
@Configuration
public class WxHttpClientConfig {

  @Resource
  private WxpayProperties wxpayProperties;

  @Bean
  @DependsOn("verifier")
  public CloseableHttpClient closeableHttpClient() throws Exception {

    String mchId= wxpayProperties.getMchId();
    String mchSerialNo = wxpayProperties.getMchSerialNo();
    PrivateKey merchantPrivateKey = null;
    ClassPathResource resource = new ClassPathResource("apiclient_key.pem");
    try {
      merchantPrivateKey = PemUtil.loadPrivateKey(
          resource.getStream());
    } catch (Exception e) {
      e.printStackTrace();
    }
    // 获取证书管理器实例
    CertificatesManager certificatesManager = CertificatesManager.getInstance();
/*// 向证书管理器增加需要自动更新平台证书的商户信息
    certificatesManager.putMerchant(mchId, new WechatPay2Credentials(mchId,
        new PrivateKeySigner(mchSerialNo, merchantPrivateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8));
// ... 若有多个商户号，可继续调用putMerchant添加商户信息*/

// 从证书管理器中获取verifier
  Verifier verifier = certificatesManager.getVerifier(mchId);
    WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
        .withMerchant(mchId, mchSerialNo, merchantPrivateKey)
        .withValidator(new WechatPay2Validator(verifier));
//        .withValidator(response ->true);
// ... 接下来，你仍然可以通过builder设置各种参数，来配置你的HttpClient
    return builder.build();
  }

  @Bean
  public CertificatesManager certificatesManager() throws Exception{
    String mchId= wxpayProperties.getMchId();
    String apiV3Key = wxpayProperties.getApiV3Key();
    String mchSerialNo = wxpayProperties.getMchSerialNo();
    PrivateKey merchantPrivateKey = null;
    ClassPathResource resource = new ClassPathResource("apiclient_key.pem");
    try {
      merchantPrivateKey = PemUtil.loadPrivateKey(
          resource.getStream());
    } catch (Exception e) {
      e.printStackTrace();
    }
    // 获取证书管理器实例
    CertificatesManager  certificatesManager = CertificatesManager.getInstance();
// 向证书管理器增加需要自动更新平台证书的商户信息
    certificatesManager.putMerchant(mchId, new WechatPay2Credentials(mchId,
        new PrivateKeySigner(mchSerialNo, merchantPrivateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8));
// ... 若有多个商户号，可继续调用putMerchant添加商户信息
    return certificatesManager;
  }

  @Bean
  @DependsOn("certificatesManager")
  public Verifier verifier() throws Exception{
    String mchId= wxpayProperties.getMchId();
    CertificatesManager  certificatesManager = CertificatesManager.getInstance();
    return certificatesManager.getVerifier(mchId);
  }
}
